package pos.storage.db.mysql;

import java.lang.ClassNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import pos.config.LoadConfig;
import pos.entity.Customer;
import pos.interfaces.Database;

/**
 * This class demonstrates the singleton design pattern (creational patterns) and returns MySQL database details
 * @author srikanth ramu
 */

public class MySQL implements Database{


    //Connect String variables
    private String url = "jdbc:mysql://";
    private String host = "localhost";
    private String port = "3306";
    private String db = "db_digishop_pos";
    
    //Customer Login details
    private String customerUser = "root";
    private String customerPasswd = "PUBLIC";

    //Clerk Login details
    private String clerkUser = "root";
    private String clerkPasswd = "PUBLIC";
    //Admin Login details
    private String adminUser = "root";
    private String adminPasswd = "PUBLIC";
    
    //Static makes one instance
    private static MySQL mysql = null;
    
    //use loadconfig class to load config hashmap
    private static LoadConfig conf = null;
    private static String configfilePath = new String("");
    
        public static String getConfigfilePath() {
        return configfilePath;
    }

    public static void setConfigfilePath(String configfilePath) {
        MySQL.configfilePath = configfilePath;
    }
            
    //restricting new object creation and providing singleton support
    private MySQL(){
        
        if(configfilePath.equalsIgnoreCase("")){
            conf = new LoadConfig("config\\config.properties");
        }else{
            conf = new LoadConfig(configfilePath);
        }
        
        url = conf.getDbParams().get("url");
        host = conf.getDbParams().get("host");
        port = conf.getDbParams().get("port");
        db = conf.getDbParams().get("database");
        
        customerUser = conf.getDbParams().get("user");
        customerPasswd = conf.getDbParams().get("pass");
        
        clerkUser = conf.getDbParams().get("user");
        clerkPasswd = conf.getDbParams().get("pass");
        
        adminUser = conf.getDbParams().get("user");
        adminPasswd = conf.getDbParams().get("pass");
    }
    
    //Creates only one instance and this is thread safe
    public static synchronized MySQL getInstance(){
	if(mysql == null){
            mysql = new MySQL();
	}
	return mysql;
    }
    
    /**
     * Creates the Database connection based on userType
     * @return
     * @throws SQLException 
     */
    public Connection getConnection(String userType) throws SQLException, ClassNotFoundException{
        String localurl = url + host + ":" + port + "/" + db;
        String localUser = null;
        String localPassword = null;
        
        if(userType.equals(Database.ADMIN)){
            localUser = adminUser;
            localPassword = adminPasswd;
        }
        else if(userType.equals(Database.CLERK)){
            localUser = clerkUser;
            localPassword = clerkPasswd;
        }
        else if(userType.equals(Database.CUSTOMER)){
            localUser = customerUser;
            localPassword = customerPasswd;
        }
                
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println(host + " " + localUser + " " + localPassword);
        Connection con = DriverManager.getConnection( localurl, localUser, localPassword);
        return con;
    }
    
    /**
     * Creates the Database connection with given connect string parameters
     * @return
     * @throws SQLException 
     */        
    public Connection getConnection(String url, String user, String passwd) throws SQLException, ClassNotFoundException{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection( url, user, passwd);
        return con;
    }
    
    
}
